<template>
  <div>
    <div :class="{'card': total>0}">
      <el-table :data="tableData" v-if="total>0">
        <el-table-column prop="name" label="名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="price" label="价格">
          <template v-slot="scope">
            <span style="color: red">￥{{ scope.row.price }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="content" label="详情" width="100">
          <template v-slot="scope">
            <el-button @click="preview(scope.row.content)">显示详情</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="发货地址" show-overflow-tooltip></el-table-column>
        <el-table-column prop="img" label="图片">
          <template v-slot="scope">
            <el-image v-if="scope.row.img" style="width: 50px" :src="scope.row.img" :preview-src-list="[scope.row.img]"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="上架日期"></el-table-column>
        <el-table-column prop="status" label="审核状态">
          <template v-slot="scope">
            <el-tag type="info" v-if="scope.row.status === '待审核'">待审核</el-tag>
            <el-tag type="success" v-if="scope.row.status === '通过'">通过</el-tag>
            <el-tag type="danger" v-if="scope.row.status === '拒绝'">拒绝</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="category" label="分类"></el-table-column>
        <el-table-column prop="saleStatus" label="上架状态"></el-table-column>
        <el-table-column prop="readCount" label="浏览量"></el-table-column>
        <el-table-column label="操作" align="center" width="240">
          <template v-slot="scope">
            <el-button size="mini" type="warning" plain @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" plain @click="del(scope.row.id,scope.row.img ? scope.row.img.substring(scope.row.img.lastIndexOf('/') + 1): '')">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination" v-if="total>pageSize">
        <el-pagination
            background
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[5, 10, 20]"
            :page-size="pageSize"
            layout="total, prev, pager, next"
            :total="total">
        </el-pagination>
      </div>
    </div>

    <el-dialog title="内容" :visible.sync="fromVisible1" width="60%" :close-on-click-modal="false" destroy-on-close>
      <div v-html="content" v-if="this.content !== '' && this.content !== null" class="w-e-text"></div>
      <div v-else class="w-e-text">暂无内容</div>
    </el-dialog>

    <el-empty style="margin-top: 20%" v-if="total<=0" description="哎呀，商品为空呢" image="http://localhost:9090/files/empty.png"></el-empty>

  </div>
</template>
<script>
export default {
  name: "FrontGoods",
  data() {
    return {
      tableData: [],  // 所有的数据
      pageNum: 1,   // 当前的页码
      pageSize: 10,  // 每页显示的个数
      total: 0,
      form: {},
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      rules: {
      },
      content: '',
      fromVisible1: false
    }
  },
  created() {
    this.load(1)
  },
  methods: {
    preview(content) {
      this.content = content
      this.fromVisible1 = true
    },
    handleEdit(row){
      this.$router.push("/front/addGoods?id="+row.id);
    },
    del(id,img) {   // 单个删除
      this.$confirm('您确定删除吗？', '确认删除', {type: "warning"}).then(response => {
        this.$request.delete('/goods/delete/' + id).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success('操作成功')
            if(img){
              this.$request.delete(`/files/${img}`)
            }
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {
      })
    },
    load(pageNum) {  // 分页查询
      if (pageNum) this.pageNum = pageNum
      this.$request.get('/goods/selectPage', {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          userId: this.user.id
        }
      }).then(res => {
        if (res.code === '200') {
          this.tableData = res.data?.list
          this.total = res.data?.total
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    reset() {
      this.name = null
      this.load(1)
    },
    handleCurrentChange(pageNum) {
      this.load(pageNum)
    },
  }
}
</script>

<style scoped>
.card {
  margin: 30px auto; /* 居中 */
  /* 背景颜色 */
  background-color: #fff;
  /* 边框样式 */
  border: 1px solid #e0e0e0;
  /* 圆角 */
  border-radius: 8px;
  /* 盒子阴影效果 */
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  /* 内边距 */
  padding: 20px;
  /* 宽度设置，可以根据实际情况调整 */
  width: 95%;
}
.empty {
  text-align: center; /* 文字居中 */
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.empty img {
  max-width: 100%;
  max-height: 80%;
  opacity: 0.5; /* 图片透明度 */
}

.empty span {
  position: relative;
  margin-top: 10px; /* 文字与图片之间的间距 */
  font-size: 15px;
  top: -100px;
}
</style>